package com.bdqn.blog.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bdqn.blog.entity.Collect;
import com.bdqn.blog.service.impl.CollectServiceImpl;
import com.bdqn.blog.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 收藏 前端控制器
 * </p>
 *
 * @author 阿桐幕
 * @since 2020-10-29
 */
@RestController
@RequestMapping("/blog/collect")
public class CollectController {

    @Autowired
    private CollectServiceImpl collectService;

    @PostMapping("/addCollect")
    public Result addCollect(@RequestBody Collect collect){
        boolean save = collectService.save(collect);
        System.out.println(save);
        if (save){
            return  Result.success();
        }else {
            return Result.fail("由于程序员太垃圾,导致出现了不知名错误！！！");
        }
    }

    @PostMapping("/getCollectOfUserid/{id}")
    public Result getCollectOfUserid(@PathVariable("id") Integer userid){
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userid);
        return Result.success(collectService.list(wrapper));
    }

    @PostMapping("/delCollect/{userid}/{songid}")
    public Result delCollect(@PathVariable("userid") Integer userid ,@PathVariable("songid") Integer songid){
        QueryWrapper<Collect> wrapper = new QueryWrapper<>();
        wrapper
                .eq("user_id",userid)
                .eq("song_id",songid);
        boolean b = collectService.remove(wrapper);
        if (b){
            return Result.success();
        }else {
            return Result.fail("由于程序员太垃圾,导致出现了不知名错误！！！");
        }
    }



}

